package com.leetcode.leetcode3;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * 合并区间
 *
 * @author luhd
 * @date 2023-08-11
 */
public class Practice56 {
    /**
     * 以数组 intervals 表示若干个区间的集合，其中单个区间为 intervals[i] = [starti, endi] 。请你合并所有重叠的区间，并返回 一个不重叠的区间数组，该数组需恰好覆盖输入中的所有区间 。
     *
     * 输入：intervals = [[1,3],[2,6],[8,10],[15,18]]
     * 输出：[[1,6],[8,10],[15,18]]
     * 解释：区间 [1,3] 和 [2,6] 重叠, 将它们合并为 [1,6].
     */
    public static void main(String[] args) {
        int[][] intervals = {{1,3},{2,6},{8,10},{15,18}};
        System.out.println(Arrays.deepToString(merge(intervals)));
    }

    public static int[][] merge(int[][] intervals) {
        if (intervals.length == 0) {
            return new int[0][0];
        }
        Arrays.sort(intervals, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return o1[0] - o2[0];
            }
        });
        List<int[]> merged = new ArrayList<>();
        int L = -1, R = -1;
        for (int i = 0; i < intervals.length; i++) {
            L = intervals[i][0];
            R = intervals[i][1];
            if (merged.isEmpty() || merged.get(merged.size()-1)[1]<L) {
                merged.add(new int[]{L, R});
            }else {
                merged.get(merged.size()-1)[1] = Math.max(merged.get(merged.size()-1)[1], R);
            }
        }
        return merged.toArray(new int[merged.size()][2]);
        // if (intervals.length == 0) {
        //     return new int[0][2];
        // }
        // Arrays.sort(intervals, new Comparator<int[]>() {
        //     public int compare(int[] interval1, int[] interval2) {
        //         return interval1[0] - interval2[0];
        //     }
        // });
        // List<int[]> merged = new ArrayList<int[]>();
        // for (int i = 0; i < intervals.length; ++i) {
        //     int L = intervals[i][0], R = intervals[i][1];
        //     if (merged.size() == 0 || merged.get(merged.size() - 1)[1] < L) {
        //         merged.add(new int[]{L, R});
        //     } else {
        //         merged.get(merged.size() - 1)[1] = Math.max(merged.get(merged.size() - 1)[1], R);
        //     }
        // }
        // return merged.toArray(new int[merged.size()][]);
    }
}